﻿//Copyright 2010 http://tv4home.codeplex.com
//This file is part of TV4Home.Client.TVViewer.
//TV4Home.Client.TVViewer is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version.
//TV4Home.Client.TVViewer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//You should have received a copy of the GNU Lesser General Public License along with TV4Home.Client.TVViewer. If not, see http://www.gnu.org/licenses/.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ServiceModel;
using System.Windows.Threading;
using System.Threading;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.ComponentModel;

namespace TV4Home.Client.Desktop.TVViewer.Controls
{
    /// <summary>
    /// Interaktionslogik für MediaPlayer.xaml
    /// </summary>
    public partial class MediaPlayer : UserControl
    {
        public static System.Timers.Timer timer = new System.Timers.Timer();
        public delegate void ChangeVisibilityDelegate();

        public MediaPlayer()
        {

            InitializeComponent();
            //Timer for hiding media element controls
            timer.Interval = 4000;
            timer.AutoReset = true;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
        }
        public void Play(string url)
        {

            meTVViewer.MediaOpened += new RoutedEventHandler(meTVViewer_MediaOpened);
            meTVViewer.Source = new Uri(url);
            if (meTVViewer.NaturalDuration.HasTimeSpan)
            {
                meTVViewer.Position = meTVViewer.NaturalDuration.TimeSpan;
            }
            meTVViewer.Play();
            btnPlayPause.Content = "Pause";
        }

        void meTVViewer_MediaOpened(object sender, RoutedEventArgs e)
        {

        }
        public void AddDescriptions(string title, string descriptiontext)
        {

            if (String.IsNullOrEmpty(title))
            {
                lblProgramTitle.Content = String.Format("Title: {0}", "no title available");
            }
            else
            {
                lblProgramTitle.Content = String.Format("Title: {0}", title);
            }

            if (String.IsNullOrEmpty(descriptiontext))
            {
                tbProgramDescription.Text = "no title available";
            }
            else
            {
                tbProgramDescription.Text = descriptiontext;
            }
        }
        public void Stop()
        {
            meTVViewer.Stop();
        }

        public TimeSpan Position
        {

            get
            {
                return meTVViewer.Position;
            }
            set
            {
                meTVViewer.Position = value;

            }
        }

        #region methods to hide player controls
        public void MouseMoved()
        {
            brdCtrlBtn.Visibility = System.Windows.Visibility.Visible;
            timer.Start();
        }
        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {

            brdCtrlBtn.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
        new ChangeVisibilityDelegate(ChangeCtrlBtnVisibility));

            timer.Stop();
        }
        private void ChangeCtrlBtnVisibility()
        {
            switch (brdCtrlBtn.Visibility)
            {
                case System.Windows.Visibility.Visible:
                    brdCtrlBtn.Visibility = Visibility.Hidden;
                    break;
                case System.Windows.Visibility.Hidden:
                    brdCtrlBtn.Visibility = Visibility.Visible;
                    break;

            }

        }
        #endregion

        #region ControlButtons

        private void btnShowDescription_Click(object sender, RoutedEventArgs e)
        {
            switch (brdDetails.Visibility)
            {
                case System.Windows.Visibility.Visible:
                    brdDetails.Visibility = Visibility.Hidden;
                    break;
                case System.Windows.Visibility.Hidden:
                    brdDetails.Visibility = Visibility.Visible;
                    break;

            }
        }

        private void btnPlayPause_Click(object sender, RoutedEventArgs e)
        {
            switch ((string)btnPlayPause.Content)
            {
                case "Play":
                    btnPlayPause.Content = "Pause";
                    meTVViewer.Play();
                    break;
                case "Pause":
                    btnPlayPause.Content = "Play";
                    meTVViewer.Pause();
                    break;
            }
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            meTVViewer.Stop();
        }

        private void sldVolume_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            meTVViewer.Volume = e.NewValue;
        }

        #endregion
    }
}
